<?php
/**
 * 
 * @author Paolo Bertuzzo
 *
 */
namespace Catalog\Data;

class StoreDao extends AbstractDao
{
    
    /**
     * 
     * @param string $country
     */
    public function get($country) {
        $query = $this->entityManager->createQuery('select s from Catalog\Entity\Store s where s.country = ?1');
        
        $query->setParameter(1, $country);
        $query->useResultCache(true);
        
        $result = $query->getResult();
        
        if (count($result) == 0)
            $this->logger->info(sprintf('Store not found for country %s', $country));

        return count($result) == 0 ? null : $result[0];
    }

    /**
     * 
     */
    public function getDefault() {
    	$query = $this->entityManager->createQuery('select s from Catalog\Entity\Store s where s.isDefault = true');
    
        $query->useResultCache(true);
    	$result = $query->getResult();
    
    	if (count($result) == 0)
    		$this->logger->warn('Default store not found');
    
    	return count($result) == 0 ? null : $result[0];
    }

    /**
     * 
     */
    public function getStores() {
        $query = $this->entityManager->createQuery('select s from Catalog\Entity\Store s');
        
        $query->useResultCache(true);
        
    	return $query->getResult();
    }
    
}

?>